package demo;

import java.io.*;
import java.util.Scanner;

public class demo2 {
    public static void main(String[] args){
        //1.输入
        Scanner in = new Scanner(System.in);
        System.out.println("输入要复制的文件路径:");
        String s1 = in.next();
        System.out.println("请输入要复制到的目录下");//包括文件的名字:也就是一个文件的绝对路径
        String s2 = in.next();
        //2.检查
        File file1 = new File(s1);
        if(!file1.isFile()) {//检查要复制的文件是否存在
            System.out.println("文件不存在");
            return;
        }
        //检查目标目录的上一级目录是否存在,file2.getParentFile()返回一个File对象，就可以直接判断该路径是否合法
        File file2 = new File(s2);
        if(!file2.getParentFile().isDirectory()) {
            System.out.println("目标路径非法");
            return;
        }

        try(InputStream inputStream = new FileInputStream(s1);OutputStream outputStream = new FileOutputStream(s2)) {
            while (true) {
                byte[] bytes = new byte[1024];
                int n = inputStream.read(bytes);
                if(n==-1) break;
                outputStream.write(bytes,0,n);
            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
